import { usePermission } from '@/hooks/usePermission'
import { setQueryParams } from '@/utils'
import { Message } from '@arco-design/web-vue'

const routeAliasConfig: any = {
  carManageDetail: {
    label: '车辆详情',
    permission: 'svms:asstes:car:car-model.detail',
    path: '/car-manage/detail',
  },
  carRepairDetail: {
    label: '维修详情',
    permission: 'svms:asstes:car:car-repair.detail',
    path: '/car-repair/detail',
  },
  carBorrowDetail: {
    label: '借调详情',
    permission: 'svms:asstes:operation:car-transfer.detail',
    path: '/car-transfer/detail',
  },
  workOrderDetail: {
    label: '工单详情',
    permission: 'svms:business:workOrder:work.details',
    path: '/work-order/detail',
  },
  shortOrderDetail: {
    label: '短租详情',
    permission: 'svms:order:short-order.detail',
    path: '/short-order/detail',
  },
  longOrderDetail: {
    label: '长租详情',
    permission: 'svms:order:long-order.detail',
    path: '/long-order/detail',
  },
  carDisposalList: {
    label: '车辆处置',
    permission: 'svms:asstes:car:disposal',
    path: '/assets/car/disposal',
  },
  violationDetail: {
    label: '违章详情',
    permission: 'svms:asstes:operation:car-violation.detail',
    path: '/car-violation/detail',
  },
  expenseFeeDetail: {
    label: '支出单详情',
    permission: 'svms:business:operateCost.detail',
    path: '/operate-cost/detail',
  },
}

const getNavConfig = (code: string, extraData: any) => {
  let option: any = {}
  let urlId: number | string = ''
  const param: any = {}

  // 车辆详情
  if (code == 'carManageDetail') {
    option = { ...routeAliasConfig['carManageDetail'] }
    const { carId, from, tab, date } = extraData
    if (carId) {
      urlId = carId
    }
    if (from) {
      param.from = from
    }
    if (tab) {
      param.tab = tab
    }
    if (date) {
      param.date = date
    }
  }

  // 车辆维修详情
  if (code == 'carRepairDetail') {
    option = { ...routeAliasConfig['carRepairDetail'] }
    const { id, status } = extraData
    if (id) {
      urlId = id
    }
    if (status) {
      param.status = status
    }
  }

  // 车辆借调详情
  if (code == 'carBorrowDetail') {
    option = { ...routeAliasConfig['carBorrowDetail'] }
    const { id, carId, type, carCode } = extraData
    if (id) {
      urlId = id
    }
    if (carId) {
      param.carId = carId
    }
    if (type) {
      param.type = type
    }
    if (carCode) {
      param.carCode = carCode
    }
  }

  // 工单详情
  if (code == 'workOrderDetail') {
    option = { ...routeAliasConfig['workOrderDetail'] }
    const { id, orderId, workCode, orderType, tab } = extraData
    if (id) {
      urlId = id
    }
    if (tab) {
      param.tab = tab
    }
    if (orderId) {
      param.orderId = orderId
    }
    if (workCode) {
      param.workCode = workCode
    }
    if (orderType) {
      param.orderType = orderType
    }
  }

  // 短租订单详情
  if (code == 'shortOrderDetail') {
    option = { ...routeAliasConfig['shortOrderDetail'] }
    const { id, carId, refCode, orderStatus } = extraData
    if (id) {
      urlId = id
    }
    if (carId) {
      param.carId = carId
    }
    if (refCode) {
      param.refCode = refCode
    }
    if (orderStatus) {
      param.orderStatus = orderStatus
    }
  }

  // 长租订单详情
  if (code == 'longOrderDetail') {
    option = { ...routeAliasConfig['longOrderDetail'] }
    const { id, carId, refCode } = extraData
    if (id) {
      urlId = id
    }
    if (carId) {
      param.carId = carId
    }
    if (refCode) {
      param.refCode = refCode
    }
  }

  // 车辆处置
  if (code == 'carDisposalList') {
    option = { ...routeAliasConfig['carDisposalList'] }
    const { licenseNoLike } = extraData
    if (licenseNoLike) {
      param.licenseNoLike = licenseNoLike
    }
  }

  // 违章详情
  if (code == 'violationDetail') {
    option = { ...routeAliasConfig['violationDetail'] }
    const { id, carId, handleType } = extraData
    if (id) {
      urlId = id
    }
    if (carId) {
      param.carId = carId
    }
    if (handleType) {
      param.handleType = handleType
    }
  }

  // 支出单详情
  if (code == 'expenseFeeDetail') {
    option = { ...routeAliasConfig['expenseFeeDetail'] }
    const { id } = extraData
    if (id) {
      urlId = id
    }
  }

  return {
    ...option,
    path: urlId ? option.path + '/' + urlId + setQueryParams(param) : option.path + setQueryParams(param),
  }
}

export const navigate2permission = (code: string, extraData: any, cb: Function, closeModal?: Function) => {
  const config = getNavConfig(code, extraData)
  const hasPermission = usePermission(config.permission)

  if (hasPermission) {
    cb(config.path, `${extraData.tabName ? extraData.tabName + '-' : ''}${config.label}`)
    if (closeModal) {
      closeModal()
    }
  } else {
    Message.warning('您没有访问该页面的权限')
  }
}

// 跳转订单详情
export const navigate2orderDetail = (record: any, navigateTo: Function, closeModal?: Function) => {
  const { id, carId, orderCode, orderStatus, orderType } = record
  if (orderType == 1) {
    navigate2permission('shortOrderDetail', { id, carId, refCode: orderCode, orderStatus, tabName: orderCode }, navigateTo, closeModal)
  } else {
    navigate2permission('longOrderDetail', { id, carId, refCode: orderCode, tabName: orderCode }, navigateTo, closeModal)
  }
}
